#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    pid_t pid = fork();
    if(pid == 0)
    {
        int i = 5;
        while(i--)
        {
            printf("i am a process!\n");
            sleep(1);
        }
        exit(0);

    }
    else
    {
        pid_t ret;
        int status;
        while(1)
        {
            ret = waitpid(pid, &status, WNOHANG);
            if(ret == 0)
            {
                printf("正在非阻塞等待\n");
                sleep(1);
            }
            else 
            {
                break;
            }
        }
        if((status & 0x7f) == 0 && ret == pid)
        {
            printf("success! child return code is %d\n", status & 0xFF00);
        }
        else 
        {
            printf("fail\n");
        }
        
    }
    return 0;
}
